﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using bzxxxt.bll;
using bzxxxt.dal;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_relative 的摘要说明
    /// </summary>
    public class bg_relative : admin.ashx.rm_base
    {
        RelativeBLL relativeBLL = new RelativeBLL();

        public override void add(HttpContext context)
        {
            string name = context.Request.Params["ui_relative_name"] ?? "";
            string cardtype = context.Request.Params["ui_relative_cardtype"] ?? "";
            string cardno = context.Request.Params["ui_relative_cardno"] ?? "";
            string gender = context.Request.Params["ui_relative_gender"] ?? "";
            string tel = context.Request.Params["ui_relative_tel"] ?? "";
            string company = context.Request.Params["ui_relative_company"] ?? "";
            string title = context.Request.Params["ui_relative_title"] ?? "";
            string addr = context.Request.Params["ui_relative_addr"] ?? "";
            string areacode = context.Request.Params["ui_relative_areacode"] ?? "";
            string postcode = context.Request.Params["ui_relative_postcode"] ?? "";
            tbRelative relative = new tbRelative
            {
                name = name,
                cardtype = cardtype,
                cardno = cardno,
                gender = gender,
                tel = tel,
                company = company,
                addr = addr,
                areacode = areacode,
                postcode = postcode,
                addtime = DateTime.Now
            };
            context.Response.Write(relativeBLL.Add(relative));

        }

        public override void edit(HttpContext context)
        {
            string id = context.Request.Params["ui_relative_id"] ?? "";
            string name = context.Request.Params["ui_relative_name"] ?? "";
            string cardtype = context.Request.Params["ui_relative_cardtype"] ?? "";
            string cardno = context.Request.Params["ui_relative_cardno"] ?? "";
            string gender = context.Request.Params["ui_relative_gender"] ?? "";
            string tel = context.Request.Params["ui_relative_tel"] ?? "";
            string company = context.Request.Params["ui_relative_company"] ?? "";
            string title = context.Request.Params["ui_relative_title"] ?? "";
            string addr = context.Request.Params["ui_relative_addr"] ?? "";
            string areacode = context.Request.Params["ui_relative_areacode"] ?? "";
            string postcode = context.Request.Params["ui_relative_postcode"] ?? "";
            tbRelative relative = new tbRelative
            {
                id = int.Parse(id),
                name = name,
                cardtype = cardtype,
                cardno = cardno,
                gender = gender,
                tel = tel,
                company = company,
                addr = addr,
                areacode = areacode,
                postcode = postcode,
                updatetime = DateTime.Now
            };
            context.Response.Write(relativeBLL.Edit(relative));
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["id"];
            context.Response.Write(relativeBLL.Delete(ids));
        }

        public override void getall(HttpContext context)
        {
            context.Response.Write(relativeBLL.GetAll());
        }

        public override void search(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(relativeBLL.GetListByPage(pageSize, pageIndex, mySort, where));             
        }

        public override void other (HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "getRelativeById":
                    {
                        string relativeId = context.Request.Params["relativeId"] ?? "0";
                        context.Response.Write(relativeBLL.GetRelativeById(int.Parse(relativeId)));
                        break;
                    }
                case "getRelativeByTel":
                    {
                        string tel = context.Request.Params["tel"] ?? "0";
                        context.Response.Write(relativeBLL.GetRelativeByTel(tel));
                        break;
                    }                               
            }
        }
    }
}